#version 460 core
#define NR_POINT_LIGHTS 4

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

out vec4 FragColor;

struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct DirLight {
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

struct PtLight {
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;
};

struct SpotLight {
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float innerCutoff;
    float outterCutoff;
};

uniform vec3 viewPos;
uniform Material material;

uniform DirLight dirLight;
uniform SpotLight spotLight;
uniform PtLight ptLight[4];

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir, vec3 texDiff, vec3 texSpec)
{
    vec3 lightDir = normalize(-light.direction);
    vec3 reflectDir = reflect(-lightDir, normal);

    float diff = max(dot(normal, lightDir), 0.0);
    float spec = pow( max( dot(viewDir, reflectDir), 0.0 ), material.shininess );

    vec3 ambient  = light.ambient  * texDiff;
    vec3 diffuse  = light.diffuse  * diff * texDiff;
    vec3 specular = light.specular * spec * texSpec;
    return (ambient + diffuse + specular);
}

vec3 CalcPtLight(PtLight light, vec3 normal, vec3 viewDir, vec3 texDiff, vec3 texSpec)
{
    vec3 lightDir = normalize( light.position - FragPos );
    vec3 reflectDir = reflect(-lightDir, normal);

    float diff = max( dot(normal, lightDir), 0.0);
    float spec = pow( max( dot(viewDir, reflectDir), 0.0 ), material.shininess );
    float dist = length( light.position - FragPos );
    float attenuation = 1.0 / ( light.constant + light.linear * dist + light.quadratic * (dist * dist) );

    vec3 ambient = light.ambient * texDiff;
    vec3 diffuse = light.diffuse * diff * texDiff;
    vec3 specular = light.specular * spec * texSpec;
    return (ambient + diffuse + specular) * attenuation;
}

vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 viewDir, vec3 texDiff, vec3 texSpec)
{
    vec3 lightDir = normalize(-light.direction);
    vec3 reflectDir = reflect(lightDir, normal);
    float theta = dot(viewDir, lightDir);
    float attenuation = clamp( (theta - light.outterCutoff)/(light.innerCutoff - light.outterCutoff), 0.0f, 1.0f);

    float diff = max(dot(normal, lightDir), 0.0f);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

    vec3 ambient = light.ambient * texDiff;
    vec3 diffuse = light.diffuse * diff * texDiff;
    vec3 specular = light.specular * spec * texSpec;
    return (ambient + diffuse + specular) * attenuation;
}

void main()
{
    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 texDiff = vec3(texture(material.diffuse, TexCoords));
    vec3 texSpec = vec3(texture(material.specular, TexCoords));

    vec3 result = CalcDirLight(dirLight, norm, viewDir, texDiff, texSpec);
    for(int i = 0; i < NR_POINT_LIGHTS; i++)
    {
        result += CalcPtLight(ptLight[i], norm, viewDir, texDiff, texSpec);
    }
    result += CalcSpotLight(spotLight, norm, viewDir, texDiff, texSpec);
    FragColor = vec4( result, 1.0);
}